home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 May / macformat-050.iso / Shareware Plus / Developers / Find_icon folder / Sources / Get_normal_file_icon.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-25  |  4.4 KB  |  163 lines  |  [TEXT/CWIE]

  1. /*    ---------------------------------------------------------------------------------------------
  2.     Find_icon, code for constructing icon suites for files and folders
  3.     
  4.     by James W. Walker
  5.     preferred e-mail: <mailto:jwwalker@kagi.com>
  6.     alternate e-mail: <mailto:jwwalker@aol.com>, <jim@nisus-soft.com>
  7.     web: <http://users.aol.com/jwwalker/>
  8.     
  9.     File: Get_normal_file_icon.c
  10.     
  11.     Copyright ©1997 by James W. Walker
  12.     
  13.     You may incorporate this sample code into your applications without
  14.     restriction, though the sample code has been provided "AS IS" and the
  15.     responsibility for its operation is 100% yours.
  16.     If you're going to re-distribute the source, please make it clear
  17.     that the code was descended from James W. Walker's code,
  18.     but that you've made changes.
  19.     ---------------------------------------------------------------------------------------------
  20. */
  21.  
  22. #include <Finder.h>
  23. #include <Folders.h>
  24. #include <Icons.h>
  25. #include <LowMem.h>
  26. #include "Get_normal_file_icon.h"
  27. #include "cheap-exceptions.h"
  28. #include "Find_generic_icon_id.h"
  29. #include "Copy_each_icon.h"
  30. #include "Get_resource_icons.h"
  31. #include "DTGetIconSuite.h"
  32.  
  33. static Boolean    Is_local_volume( short vRefNum )
  34. {
  35.     GetVolParmsInfoBuffer    vol_parms;
  36.     OSErr                    err;
  37.     HIOParam                opb;
  38.  
  39.     opb.ioBuffer = (Ptr)&vol_parms;
  40.     opb.ioReqCount = sizeof(vol_parms);
  41.     opb.ioVRefNum = vRefNum;
  42.     opb.ioNamePtr = NULL;
  43.     err = PBHGetVolParmsSync( (HParmBlkPtr) &opb );
  44.     
  45.     return (err != noErr) || (vol_parms.vMServerAdr == 0);
  46. }
  47.  
  48. /*    ------------------------------------------------------------------
  49.     Get_normal_file_icon        Get an icon suite for a file that does
  50.                                 not have a custom icon.
  51.     ------------------------------------------------------------------
  52. */
  53.  
  54.  
  55. OSErr    Get_normal_file_icon(
  56. /* --> */    const CInfoPBRec    *cpb,
  57. /* --> */    MetaSelectorValue    icon_selector,
  58. /* <-- */    Handle    *the_suite
  59. )
  60. {
  61.     short    icon_id;
  62.     EGenericIconLocation    inWhere;
  63.     short    save_resfile, Finder_resfile, sys_vRefNum;
  64.     long    sys_dirID;
  65.     OSErr    err;
  66.     short    orig_vRefNum;
  67.     IconSelectorValue    second_selector;
  68.     VolumeParam        vpb;
  69.     OSType    fileType = cpb->hFileInfo.ioFlFndrInfo.fdType;
  70.     
  71.     ExpandMetaSelector( icon_selector, &icon_selector, &second_selector );
  72.     
  73.     icon_id = Find_generic_icon_id( fileType, &inWhere );
  74.     save_resfile = CurResFile();
  75.     
  76.     if (inWhere == kGenericIconInFinder)
  77.     {
  78.         (void) FindFolder( kOnSystemDisk, kSystemFolderType,
  79.               kDontCreateFolder, &sys_vRefNum, &sys_dirID );
  80.         SetResLoad( false );
  81.         Finder_resfile = HOpenResFile( sys_vRefNum, sys_dirID,
  82.             LMGetFinderName(), fsRdPerm );
  83.         SetResLoad( true );
  84.         forbid_action( Finder_resfile == -1, HOpenResFile,
  85.             err = ResError() );
  86.         err = Get1_resource_icons( the_suite, icon_id, icon_selector );
  87.         if ( (*the_suite == NULL) && (second_selector != 0) )
  88.         {
  89.             err = Get1_resource_icons( the_suite, icon_id, second_selector );
  90.         }
  91.         CloseResFile( Finder_resfile );
  92. HOpenResFile:    ;
  93.     }
  94.     else    // icons in desktop DB or in System
  95.     {
  96.         if (fileType == kApplicationAliasType)
  97.         {
  98.             fileType = 'APPL';
  99.         }
  100.         
  101.         orig_vRefNum = cpb->hFileInfo.ioVRefNum;
  102.         err = DTGetIconSuite( orig_vRefNum, icon_selector,
  103.             cpb->hFileInfo.ioFlFndrInfo.fdCreator,
  104.             fileType,
  105.             the_suite );
  106.         if ( (*the_suite == NULL) && (second_selector != 0) )
  107.         {
  108.             err = DTGetIconSuite( orig_vRefNum, second_selector,
  109.                 cpb->hFileInfo.ioFlFndrInfo.fdCreator,
  110.                 fileType,
  111.                 the_suite );
  112.         }
  113.         if (err != noErr)    // try any other local volumes
  114.         {
  115.             vpb.ioNamePtr = NULL;
  116.             for (vpb.ioVolIndex = 1;
  117.                 PBGetVInfoSync( (ParmBlkPtr) &vpb ) == noErr;
  118.                 ++vpb.ioVolIndex )
  119.             {
  120.                 if ( (vpb.ioVRefNum == orig_vRefNum)
  121.                     || !Is_local_volume( vpb.ioVRefNum ) )
  122.                 {
  123.                     continue;
  124.                 }
  125.                 err = DTGetIconSuite( vpb.ioVRefNum, icon_selector,
  126.                     cpb->hFileInfo.ioFlFndrInfo.fdCreator,
  127.                     fileType,
  128.                     the_suite );
  129.                 if ( (*the_suite == NULL) && (second_selector != 0) )
  130.                 {
  131.                     err = DTGetIconSuite( vpb.ioVRefNum,
  132.                         second_selector,
  133.                         cpb->hFileInfo.ioFlFndrInfo.fdCreator,
  134.                         fileType,
  135.                         the_suite );
  136.                 }
  137.                 if ( err == noErr )
  138.                     break;
  139.             }
  140.         }
  141.         
  142.         if (err != noErr) // resort to generic icon from System
  143.         {
  144.             UseResFile( 0 );
  145.             err = Get1_resource_icons( the_suite, icon_id, icon_selector );
  146.             if ( (*the_suite == NULL) && (second_selector != 0) )
  147.             {
  148.                 err = Get1_resource_icons( the_suite, icon_id, second_selector );
  149.             }
  150.             if (*the_suite == NULL)
  151.             {
  152.                 err = Get1_resource_icons( the_suite, genericDocumentIconResource,
  153.                     icon_selector );
  154.             }
  155.         }
  156.     } // end else not in Finder
  157.  
  158.     UseResFile( save_resfile );
  159.  
  160.     return err;
  161. }
  162.  
  163.